<template>
	<view class="sp-section sp-room">
		<view class="sp-room__build sp-index__history" style="padding-bottom: 40rpx;">
			<view class="sp-title">
				<u-cell class="sp-oa__boxes-name" :title="farm.farm_name" arrow-direction="down" @click="farmShow = true">
					<text slot="value">
						<u-icon name="arrow-down" size="24" />
					</text>
				</u-cell>
			</view>
			<template v-if="build && build.length">
				<view
					v-for="item in build"
					:key="item.id"
					class="sp-room__build-item"
					:class="{
						'danger': item.farm_scene != buildNums[item.id]
					}"
					@click="toCameraDetail(item)"
				>
					<view class="sp-room__build-name">{{ item.farm_name }}</view>
					<view class="sp-room__build-info">
						<view class="sp-room__build-desc">
							<label>栏位</label>
							<text>{{ item.farm_scene ? item.farm_scene.length : '0' }}个</text>
						</view>
						<view class="sp-room__build-desc">
							<label>有猪栏位</label>
							<text>{{ item.hasPigs && item.hasPigs.length ? item.hasPigs.length : 0 }}个</text>
						</view>
						<view class="sp-room__build-desc">
							<label>今日拍照栏位</label>
							<text>{{ buildNums[item.id] ? buildNums[item.id] : 0 }}个</text>
						</view>
					</view>
				</view>
			</template>
			<template v-else>
				<sp-empty />
			</template>
			<!-- 选择农场 -->
			<!-- <u-picker mode="region" :show="farmShow1" :area-code='farmList' range-key="name" @confirm="getBuildByFarm"></u-picker> -->
			<!-- 选择农场 -->
			<u-picker :show="farmShow" :columns="[farmList]" keyName="farm_name" closeOnClickOverlay @close="farmShow = false" @confirm="getBuildByFarm" />
			<!-- <u-action-sheet :actions="farmList" title="选择农场" :show="farmShow" closeOnClickOverlay @close="farmShow = false"  @select="getBuildByFarm"></u-action-sheet> -->
			<!-- <template v-for="bd in build">
				<view class="sp-title" :key="bd.id" style="margin-top: 30rpx;">
					<u-cell class="sp-oa__boxes-name" :title="bd.farm_name"></u-cell>
				</view>
				<template v-if="bd.children && bd.children.length">
					<view
						v-for="item in bd.children"
						:key="item.id"
						class="sp-room__build-item"
						:class="{
							'danger': item.farm_scene != buildNums[item.id]
						}"
						@click="$u.route('/pages/room/detail', { id: item.id })"
					>
						<view class="sp-room__build-name">{{ item.farm_name }}</view>
						<view class="sp-room__build-info">
							<view class="sp-room__build-desc">
								<label>栏位</label>
								<text>{{ item.farm_scene ? item.farm_scene.length : '0' }}个</text>
							</view>
							<view class="sp-room__build-desc">
								<label>有猪栏位</label>
								<text>{{ item.hasPigs && item.hasPigs.length ? item.hasPigs.length : 0 }}个</text>
							</view>
							<view class="sp-room__build-desc">
								<label>今日拍照栏位</label>
								<text>{{ buildNums[item.id] ? buildNums[item.id] : 0 }}个</text>
							</view>
						</view>
					</view>
				</template>
				<template v-else>
					<sp-empty />
				</template>
			</template> -->
		</view>
	</view>
</template>

<script>
	import dayjs from '@/utils/dayjs.min.js'
	import { getRecordsById, getRecordParams } from '@/api/task.js'
	import { getFarm } from '@/api/alot.js'
	
	export default {
		name: 'spIndexData',
		data() {
			return {
				date: '',
				farm: {},
				farmList: [],
				farmShow: false,
				farmShow1: true,
				build: [],
				buildTree: [],
				buildNums: {},
				// 类型 1 料槽 2 背膘
				type: 1
			}
		},
		onLoad(options) {
			this.type = options.type || 1
		},
		onShow() {
			this.date = dayjs().format('YYYY-MM-DD')
			// this.getFarmInfo()
			if (this.buildTree && this.buildTree.length) {
				this.getCameraList()
			} else {
				this.getFarmInfo()
			}
			const users = uni.getStorageSync('aiUserInfo')
			console.log('enterprise_id', users.enterprise_id)
		},
		mounted() {			
			// this.getRecordsAsBuild()
			this.getFarmInfo()
		},
		methods: {
			async getRecordsAsBuild() {
				const data = await getRecordParams()
				console.log('统计数据', data)
			},
			toCameraDetail(item) {
				this.$u.route(`/pages/room/${this.type == 1 ? 'detail' : 'rate'}`, { id: item.id, name: item.farm_name, type: this.type })
			},
			async getCameraList() {
				// this.date = '2024-06-13'
				const data = await getRecordsById({
					page: 1,
					page_size: 4999,
					start_time: `${this.date} 00:00:00`,
					end_time: `${this.date} 23:59:59`
				})
				const test = data.records.filter(item => [3015, 3016, 3017].includes(item.building_id))
				console.log('test', test)
				if (data.records && data.records.length) {
					const buildIds = Array.from(new Set(data.records.map(item => item.building_id)))
					const records = data.records.map(item => ({ bid: item.building_id, cid: item.column_id }))
					const uniqueCids = new Set();  
					const result = records.filter(item => {
					  if (!uniqueCids.has(item.cid)) {
					    uniqueCids.add(item.cid);
					    return true;
					  }
					  return false;
					});
					console.log('records', result)
					const obj = {}
					buildIds.forEach(item => {
						obj[item] = result.filter(dv => dv.bid === item).length
					})
					this.buildNums = obj
				}
			},
			// 获取农场数据
			getFarmInfo() {
				const token = uni.getStorageSync('token')
				const users = uni.getStorageSync('aiUserInfo')
				console.log('enterprise_id', users.enterprise_id)
				uni.request({
					url: `${getFarm}?institute=${users.enterprise_id}&phone=${users.username}&search_farmtype__name=料槽`,
					method: 'get',
					dataType: 'json',
					header: {
						authorization: token
					},
					success: (res) => {
						console.log('获取农场数据', res)
						const list = res.data.data
						if (list && list.length) {
							const build = list.filter((item) => item.level === 1)
							const farm = list.filter((item) => item.level === 0)
							this.farm = farm && farm.length ? farm[0] : {}
							const buildList = build && build.length ? build.map(item => {
								item.hasPigs = item.farm_scene.filter(item => item.has_pig)
								return item
							}) : []
							// const parentIds = Array.from(new Set(buildList.map(item => item.parent)))
							console.log(buildList)
							this.buildTree = farm.map(item => {
								item.children = build.filter(dv => dv.parent == item.id)
								return item
							})
							this.build = this.buildTree && this.buildTree.length ? this.buildTree[0].children : []
							this.farmList = farm.map(item => {
								item.name = item.farm_name
								return item
							})
							this.getCameraList()
						}
					}
				})
			},
			// 根据农场id获取农场数据
			getBuildByFarm(e) {
				console.log('e', e)
				// return false..value[0]
				this.farm = e.value[0]
				// console.log('选择的农场', e.value[0])
				const build = this.buildTree.find(item => item.id == e.value[0].id)
				// console.log('build', build)
				this.build = build?.children ?? []
				this.farmShow = false
				this.getCameraList()
			}
		}
	}
</script>

<style lang="scss">
	.sp-room__build {
		
		&- {
			
			&item {
				width: 700rpx;
				height: 160rpx;
				padding: 0 30rpx;
				box-sizing: border-box;
				margin: 25rpx auto 0;
				background: #846BFE;
				box-shadow: 0px 8rpx 20rpx 0px rgba(155,97,255,0.28);
				border-radius: 24rpx;
				transition: all .3s;
				color: #fff;
				
				&.danger {
					background: rgba(247,94,78,0.63);
				}
			}
			
			&name {
				height: 60rpx;
				line-height: 60rpx;
				border-bottom: 1rpx solid #fff;
				box-sizing: border-box;
				font-size: 28rpx;
			}
			
			
			&desc {
				position: relative;
				flex: 1;
				font-size: 12rpx;
				text-align: center;
				
				&:not(:first-child):after {
					content: '';
					position: absolute;
					top: 15rpx;
					left: 0;
					width: 1rpx;
					height: 50rpx;
					background: #fff;
				}
			}
			
			&info {
				display: flex;
				width: 100%;
				// justify-content: space-between;
				height: 100rpx;
				padding: 10rpx 0;
				box-sizing: border-box;
				
				label,
				text {
					display: block;
					line-height: 40rpx;
				}
				
				label {
					font-size: 28rpx;
				}
				
				text {
					font-size: 24rpx;
					text-align: center;
				}
			}
		}
	}
</style>